import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';

import 'package:ball_lion/resources/colours.dart';
import 'base/wqs_text.dart';
import 'base/wqs_dialog.dart';

class TipWidget extends StatefulWidget {
  const TipWidget({
    super.key,
    required this.type,
    this.fontSize = 12,
    this.textColor = Colours.grey99,
    this.lineHeight = 1.2,
    this.linkColor = Colours.main,
  });

  final String type;
  final double fontSize;
  final Color textColor;
  final double lineHeight;
  final Color linkColor;

  @override
  State<TipWidget> createState() => _TipWidgetState();
}

class _TipWidgetState extends State<TipWidget> {
  ResourceEntity? tip;

  @override
  void initState() {
    getTip();
    super.initState();
  }

  Future getTip() async {
    var data = await OtherApi.getAppList("app_res_tips");
    tip = data?.firstWhere((element) => element.button == widget.type,
        orElse: () => ResourceEntity());
    if (tip?.content != null && tip!.content!.contains("<")) {
      tip?.content = "<body>${tip?.content}</body>";
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return tip?.content == null
        ? Container()
        : Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (tip!.content!.contains("<")) ...[
                Html(
                  data: tip?.content,
                  style: {
                    "body": Style(margin: Margins.all(0)),
                    "p": Style(
                      fontSize: FontSize(widget.fontSize),
                      color: widget.textColor,
                      margin: Margins.all(0),
                      lineHeight: LineHeight(widget.lineHeight),
                      textAlign: TextAlign.justify,
                    ),
                    "a": Style(
                        color: widget.linkColor,
                        textDecoration: TextDecoration.none)
                  },
                  onLinkTap: (url, context, element) async {
                    if (url != null) {
                      if (url == "/contact/wx") {
                        var result = await Get.dialog(QsDialog.alertBUTTON(
                          "",
                          content:
                              "您好，您可以在周一至周五的09:00-18:00通过微信客服联系我们，非工作时间您可以在“我的”页面--建议反馈中留言，我们会在工作时间尽快回复!",
                          confirmText: "联系客服",
                        ));
                        if (result == true) {
                          Fluwx().open(
                              target: CustomerServiceChat(
                                  corpId: "wwdcd535ec00a4b23a",
                                  url:
                                      "https://work.weixin.qq.com/kfid/kfca3c7d9a3e4b50ccf"));
                        }
                      } else {
                        Get.toNamed(url);
                      }
                    }
                  },
                ),
              ] else ...[
                QsText(tip!.content!,
                    fontSize: widget.fontSize,
                    color: widget.textColor,
                    height: widget.lineHeight)
              ]
            ],
          );
  }
}
